<template>
    <view>
        <view class="sift" @click="show=true">
            <div>筛选日期：
                <span :style="{color:!time.length?'#333333':'#009977'}">{{ !time.length ? '全部' : time.join(' - ') }}</span>
            </div>
            <image src="https://smosun-minapp.oss-cn-shanghai.aliyuncs.com/weapp/img/down.png"/>
        </view>
        <u-popup :show="show" @close="onClose" round="20" closeable>
            <view class="container">
                <div class="'title">筛选时间</div>
                <div class="range">
                    <div :class="{active:active===0}" @click="active=0">{{ range[ 0 ] || '开始时间' }}</div>
                    <span>至</span>
                    <div :class="{active:active===1}" @click="active=1">{{ range[ 1 ] || '结束时间' }}</div>
                </div>
                <picker-view :value="getValue(range[active])" class="picker" @change="onChange">
                    <picker-view-column>
                        <view v-for="(v,k) in years" :key="k" class="column">{{ v }}年</view>
                    </picker-view-column>
                    <picker-view-column>
                        <view v-for="(v,k) in months" :key="k" class="column">{{ v }}月</view>
                    </picker-view-column>
                    <picker-view-column>
                        <view v-for="(v,k) in days" :key="k" class="column">{{ v }}日</view>
                    </picker-view-column>
                </picker-view>
            </view>
            <div class="btn-row">
                <div class="reset" @click="onReset">
                    <image src="https://smosun-minapp.oss-cn-shanghai.aliyuncs.com/weapp/img/reset.png"/>
                    <div>重置</div>
                </div>
                <div class="btn" @click="onConfirm">确定</div>
            </div>
        </u-popup>
    </view>
</template>

<script>
import moment from 'moment';

export default {
    name: 'date-time-picker',
    props: {
        rangeStart: {
            type: Number,
            default: 1990
        },
        rangeEnd: {
            type: Number,
            default: moment().year()
        },
        time: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            show: false,
            range: [],
            active: 0,
            years: [],
            months: [],
            days: []
        };
    },
    watch: {
        show(bool) {
            bool && this.onReset();
        }
    },
    methods: {
        getDays(m) {
            const days = moment(m, 'M').daysInMonth();
            const arr = [];
            for (let i = 1; i <= days; i++) {
                arr.push(i);
            }
            this.days = arr;
        },
        onChange({ detail: { value } }) {
            const { range, active, months } = this;
            const pTime = range[ active ];
            this.range.splice(active, 1, this.format(value));
            this.value = value;
            const m = months[ value[ 1 ] ];
            (!pTime || m !== moment(pTime, 'YYYY/MM/DD').month() + 1) && this.getDays(m);
        },
        format(arr) {
            const { years, months, days } = this;
            return [ years, months, days ].map((item, i) => this.addZero(item[ arr[ i ] ])).join('/');
        },
        addZero(val) {
            return val < 10 ? `0${val}` : val;
        },
        getValue(date) {
            if (!date) {
                return [];
            }
            const time = moment(date);
            const arr = [
                time.year(),
                time.month() + 1,
                time.date()
            ];
            const { years, months, days } = this;
            return [ years, months, days ].map((item, i) => item.findIndex(it => it === arr[ i ]));
        },
        onConfirm() {
            const [ s, e ] = this.range;
            if (!s) {
                return uni.showToast({ title: '开始时间不能为空', icon: 'none' });
            }
            if (!e) {
                return uni.showToast({ title: '结束时间不能为空', icon: 'none' });
            }
            if (moment(s).isAfter(moment())) {
                return uni.showToast({ title: '开始时间需小于当前时间', icon: 'none' });
            }
            if (moment(s).isAfter(moment(e))) {
                return uni.showToast({ title: '开始时间需小于结束时间', icon: 'none' });
            }
            this.$emit('confirm', this.range);
            this.onClose();
        },
        onClose() {
            this.range = [];
            this.active = 0;
            this.show = false;
        },
        onReset() {
            if (!this.time.length) {
                return this.range = [ moment().format('YYYY/MM/DD'), moment().format('YYYY/MM/DD') ];
            }
            this.range = [ ...this.time ];
        }
    },
    created() {
        const { rangeStart, rangeEnd, years, months } = this;
        for (let i = rangeStart; i <= rangeEnd; i++) {
            years.push(i);
        }
        for (let i = 1; i <= 12; i++) {
            months.push(i);
        }
        this.getDays(moment().month() + 1);
    }
};
</script>

<style scoped lang="scss">
.sift {
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    background: #F3F7F6;
    border-radius: 25px;
    align-items: center;
    margin: 0 15px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    image {
        width: 16px;
        height: 16px;
    }
}
.container {
    border-radius: 12px 12px 0px 0px;
    padding: 15px 15px 60px;
    background: #FFFFFF;
    .title {
        font-size: 17px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #242532;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .range {
        display: flex;
        align-items: center;
        padding: 20px 0 35px;
        border-bottom: 1px solid transparent;
        div {
            background: #FFFFFF;
            border-radius: 10px;
            border: 1px solid #DFDFDF;
            flex: 1;
            font-size: 14px;
            font-family: PingFangSC-Semibold, PingFang SC;
            font-weight: 600;
            color: #333333;
            display: flex;
            align-items: center;
            padding: 13px;
        }
        span {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #2B2C30;
            margin: 0 10px;
        }
        .active {
            border: 1px solid #009977;
            color: #009977;
        }
    }
    .picker {
        height: 200px;
    }
    .column {
        line-height: 40px;
        text-align: center;
    }
}
.btn-row {
    display: flex;
    align-items: center;
    padding: 10px 15px;
    box-shadow: 0px -1px 0px 0px rgba(38, 43, 49, 0.06);
    border-radius: 20px 20px 0px 0px;
    background: #FFFFFF;
    margin-top: -20px;
    .reset {
        font-size: 10px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #666666;
        margin-right: 40px;
        text-align: center;
        image {
            width: 24px;
            height: 24px;
        }
    }
    .btn {
        height: 48px;
        background: linear-gradient(180deg, #46C09E 0%, #156D4F 100%);
        box-shadow: 0px 12px 24px 0px rgba(55, 168, 148, 0.2);
        border-radius: 12px;
        flex: 1;
        font-size: 15px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #FFFFFF;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
</style>
